
package com.trial.area.common.tags;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.trial.area.common.SpringBeansTool;
import com.trial.area.dictionary.entity.DictionaryVO;
import com.trial.area.dictionary.service.DictionaryService;

/**
 * 根据字典数据配置生成枚举的checkBox，传入值默认选中对应项
 *
 * @author 朱国军
 * @since 1.0
 * @version 2016年3月28日 朱国军
 */
public final class DictionaryCheckBoxTag extends TagSupport {

    private static Logger logger = Logger.getLogger(DictionaryCheckBoxTag.class);

    private static final long serialVersionUID = -7177581356703711990L;
    
    private DictionaryService dictionaryService = SpringBeansTool.context.getBean(DictionaryService.class);
    
    private String fieldName;

    private String tableColumn;

    private String describe;

    private String value;
    
    /**
     * 获取枚举字段名称
     *
     * @return 枚举字段名称
     */
    public String getFieldName() {
        return fieldName;
    }
    
    /**
     * 设置枚举名称
     *
     * @param fieldName 枚举名称
     */
    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }
    
    /**
     * 获取枚举值key
     *
     * @return 枚举值key
     */
    public String getTableColumn() {
        return tableColumn;
    }
    
    /**
     * 设置枚举值key
     *
     * @param tableColumn 枚举值key
     */
    public void setTableColumn(String tableColumn) {
        this.tableColumn = tableColumn;
    }

    /**
     * 获取枚举字段描述
     *
     * @return 枚举字段描述
     */
    public String getDescribe() {
        return describe;
    }
    
    /**
     * 设置枚举描述
     *
     * @param name 枚举描述
     */
    public void setDescribe(String describe) {
        this.describe = describe;
    }
    
    /**
     * 获取枚举值
     *
     * @return 枚举值
     */
    public String getValue() {
        return value;
    }
    
    /**
     * 设置枚举值
     *
     * @param value 枚举值
     */
    public void setValue(String value) {
        this.value = value;
    }
    
    @Override
    public int doEndTag() throws JspException {
        return DictionaryCheckBoxTag.EVAL_PAGE;
    }

    @Override
    public int doStartTag() throws JspException {
        List<DictionaryVO> enumList = dictionaryService.queryList(tableColumn);
        return writeEditPage(enumList);
    }

    private int writeEditPage(List<DictionaryVO> enumList) {
        StringBuilder builder = new StringBuilder();
        if (enumList != null && enumList.size() > 0) {
            int enumSize = enumList.size();
            builder.append("<div class='form-group'>");
            builder.append("<label class='col-sm-1 control-label'>").append(describe).append("<em>*</em></label>");
            builder.append("<div class='col-sm-10'>");
            for (int i = 0; i < enumSize; i++) {
                String radioId = fieldName + "_" + i;
                builder.append("<label class='checkbox-inline'><input type='checkbox' name='").append(fieldName).append("' id='").append(radioId)
                    .append("' ");
                builder.append("value='").append(enumList.get(i).getItemName()).append("' ");
                if (value != null && StringUtils.contains(value, "," + enumList.get(i).getItemName() + ",")) {
                    builder.append("checked='true' ");
                }
                builder.append(">").append(enumList.get(i).getItemName()).append("</label>");
            }
            builder.append("</div></div>");
        } else {
            logger.error("字典项转radio的Tag出错，根据tableColumn：" + tableColumn + "未找到对应数据字典");
            builder.append("error--根据tableColumn：" + tableColumn + "未找到对应数据字典");
        }
        JspWriter writer = this.pageContext.getOut();
        try {
            writer.write(builder.toString().trim());
        } catch (IOException e) {
            logger.error("字典项转radio的Tag出错", e);
        }
        return DictionaryCheckBoxTag.SKIP_BODY;
    }
}
